//
//  LZUserAccount.swift
//  Weibo
//
//  Created by lzing on 16/3/23.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit

class LZUserAccount: NSObject,NSCoding {
    /// 调用access_token,获取授权access_token
    var access_token : String?
    /// 过期时间
    var expires_in : NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 过期时间,Date
    var expiresDate : NSDate?
    /// 当前授权的uid
    var uid : String?
    
    //MARK: - 用户信息
    // 用户名称
    var screen_name : String?
    // 用户头像地址
    var avatar_large : String?
    
    // 字典转模型
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    /** 拦截未设置属性*/
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // 重写description
    override var description : String {
        get {
            let keys = ["access_token", "expires_in", "expiresDate", "uid"]
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    
    // MARK: - encode&&decoder
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
